package cn.sdxx.qddata.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author libin
 */
@Data
@Accessors(chain = true)
@ApiModel("统一响应结果对象")
public class R {
    @ApiModelProperty("状态码")
    private int code;

    @ApiModelProperty("状态信息")
    private String msg;

    @ApiModelProperty("数据")
    private Object data;

    public static R success(int code, String msg, Object data){
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static R success(String msg,Object data){
        R result = new R();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static R success(Object data){
        R result = new R();
        result.setCode(200);
        result.setMsg("操作成功");
        result.setData(data);
        return result;
    }

    /**
     * 操作失败（带数据）
     * @param code
     * @param msg
     * @return
     */
    public static R fail(int code, String msg, Object data){
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 操作失败（不带数据）
     * @param code
     * @param msg
     * @return
     */
    public static R fail(int code, String msg){
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }
    
    public static R fail(String msg) {
        R result = new R();
        result.setCode(-1);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }

}
